package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"profile/model/mysql"

	"profile/userrpc/internal/svc"
	"profile/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *userrpc.UserUpdateRequest) (*userrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.User
	mysql.DB.Where("id = ?", in.Id).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("该账号不存在")
	}
	user := mysql.User{
		Model:    gorm.Model{ID: finduser.ID},
		Sex:      in.Sex,
		Birthday: in.Birthday,
	}
	mysql.DB.Updates(&user)

	return &userrpc.UserUpdateResponse{
		Id: int64(user.ID),
	}, nil
}
